package org.wenhuayuan.project.main.biz

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.android.synthetic.main.activity_login.*
import org.wenhuayuan.library.resful.HiCallback
import org.wenhuayuan.library.resful.HiResponse
import org.wenhuayuan.project.common.router.ARouterPath
import org.wenhuayuan.project.common.uicomp.HiBaseActivity
import org.wenhuayuan.project.common.util.SPUtil
import org.wenhuayuan.project.main.R
import org.wenhuayuan.project.main.http.ApiFactory
import org.wenhuayuan.project.main.http.api.AccountApi
@Route(path = "/account/login")
class LoginActivity : HiBaseActivity() {
    private val REQUEST_CODE_REGISTRATION = 1000
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        action_back.setOnClickListener {
            onBackPressed()
        }

        action_register.setOnClickListener {
            goRegisteration()
        }
        action_login.setOnClickListener {
            goLogin()
        }
    }

    private fun goLogin() {
        val name = input_item_username.getEditText().text
        input_item_password.getEditText().setText("123456")
        val password = input_item_password.getEditText().text

        if (TextUtils.isEmpty(name) or TextUtils.isEmpty(password)) {
            return
        }
        ApiFactory.create(AccountApi::class.java)
            .login(name.toString(), password.toString())
            .enqueue(object: HiCallback<String>{
                override fun onSuccess(response: HiResponse<String>) {
                    if (response.code == HiResponse.SUCCESS) {
                        showToast("onSuccess@登录成功")
                        SPUtil.putString("boarding-pass", response.data)
                        Log.e("登录请求成功", "response.data: "+response.data)
                        setResult(Activity.RESULT_OK, Intent())
                        finish()
                    }
                }

                override fun onFailed(throwable: Throwable) {
                    Log.e("登录请求失败", "")
                }
            })
    }

    private fun goRegisteration() {
        ARouter.getInstance().build(ARouterPath.AROUTER_PATH_USER_REGISTRATION).navigation(this, REQUEST_CODE_REGISTRATION)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let { intent ->
            val userName = intent.getStringExtra("username")
            if((resultCode == Activity.RESULT_OK) and (requestCode == REQUEST_CODE_REGISTRATION) and !TextUtils.isEmpty(userName)) {
                input_item_username.getEditText().setText(userName)
            }
        }

    }
    private fun showToast(msg: String?) {
        msg?.let {
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
        }
    }
}
